//
//  HGMusicViewController.m
//  悦享
//
//  Created by dllo on 16/3/21.
//  Copyright © 2016年 张洪国. All rights reserved.
//

#import "HGMusicViewController.h"
#import "DCPicScrollView.h"
#import "DCWebImageManager.h"
#import "SecViewController.h"
#import "HGMusicLunBoTwoViewController.h"
#import "HGMusicOneCollectionViewCell.h"
#import "HGMusicHeaderCollectionReusableView.h"
#import "HGMusicTwoCollectionViewCell.h"
#import "HGMusicThreeCollectionViewCell.h"
#import "HGMusicFourCollectionViewCell.h"
#import "HGMvViewController.h"
#import "SongModel.h"
#import "PlayMusciViewController.h"
#import "HGMusicSecViewController.h"
#import "HSViewController.h"
#import <MBProgressHUD.h>
#import "HGNewMusicViewController.h"
#import "HGMVsecViewController.h"
@interface HGMusicViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) NSMutableArray *dataArr;/**<  数据源 */
@property (nonatomic, retain) UICollectionView *collection;/**<  底层collection */

@property (nonatomic, retain) DCPicScrollView *dcPicView;/**<  轮播视图 */
@property (nonatomic, retain) UITableView *playTableView;/**<  播放tableView */

@end

@implementation HGMusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self createCollection];
    
    [self handelData];
    
    [self createTiShiLabel];
    
    //播放通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"play" object:nil];
    
    //跳转到播放界面通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jumpToPlayMusicVC:) name:@"jumpToVC" object:nil];
    
    //跳转到mv播放界面的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mvVcAction:) name:@"mvPostToVC" object:nil];
    
    
    /**<  开启小菊花 */
    [MBProgressHUD showHUDAddedTo:_collection animated:YES];
    
    
}
- (void)mvVcAction:(NSNotification *)notification{
    
    HGMvViewController *hgmv = [[HGMvViewController alloc] init];
    hgmv.model = notification.object;
    
    [self presentViewController:hgmv animated:YES completion:^{
        
    }];
    
}

//跳转页面
- (void)jumpToPlayMusicVC:(NSNotification *)notification{
   
    
   
    PlayMusciViewController *playMusic = [[PlayMusciViewController alloc] init];
    playMusic.model = (SongModel *)notification.object;
   
    playMusic.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:playMusic animated:YES completion:^{
        
    }];
    
    
}

- (void)createLunBoTuView{
    HGMusicModel *model = [_dataArr firstObject];
    
    NSMutableArray *picArr = [NSMutableArray array];
    
    for (HGMusicDataModel *model1 in model.dataArr) {
        [picArr addObject:model1.picUrl];
    }
    
    self.dcPicView = [DCPicScrollView picScrollViewWithFrame:CGRectMake(0, 0, SREEN_WIDTH, 200) WithImageUrls:picArr];
    _dcPicView.AutoScrollDelay = 3;
    
    [_collection addSubview:_dcPicView];
    
   
    
    _dcPicView.imageViewDidTapAtIndex = ^(NSInteger index){
        HGMusicDataModel *model2 = model.dataArr[index];
        NSDictionary *dic = model2.action;
        
        if ([[dic[@"type"] stringValue] isEqualToString:@"1"]) {
            NSString *urlStr = dic[@"value"];
            
            SecViewController *sec = [[SecViewController alloc] init];
            sec.url = urlStr;
            
            [self presentViewController:sec animated:YES completion:^{
                
            }];
            
            
        }else if ([[dic[@"type"] stringValue] isEqualToString:@"5"]){
            
            HGMusicLunBoTwoViewController *hgmusicTwo = [[HGMusicLunBoTwoViewController alloc] init];
            
            hgmusicTwo.url = dic[@"value"];
            
            [self.navigationController pushViewController:hgmusicTwo animated:YES];
            
        }
        
        
    };
    

}
///创建提示下拉返回
- (void)createTiShiLabel{
    
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(0, -100, SREEN_WIDTH, 50);
    label.text = @" ⬇️ 下拉返回";
    label.textColor = [UIColor blueColor];
    label.textAlignment = NSTextAlignmentCenter;
    
    [_collection addSubview:label];
    
    
}
///返回方法炒作
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (_collection.contentOffset.y < -200) {
        [self.navigationController popToRootViewControllerAnimated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"removeTableView" object:nil];
        
         }
}
///创建底层collectionview
- (void)createCollection{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10 ;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SREEN_WIDTH, SREEN_HEIGHT - 45) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collection];
    
    [_collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    
    [_collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionReusableView"];
    
    [_collection registerClass:[HGMusicHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HGMusicHeaderCollectionReusableView"];
    
    [_collection registerClass:[HGMusicOneCollectionViewCell class] forCellWithReuseIdentifier:@"HGMusicOneCollectionViewCell"];
    
    [_collection registerClass:[HGMusicTwoCollectionViewCell class] forCellWithReuseIdentifier:@"HGMusicTwoCollectionViewCell"];
    
    [_collection registerClass:[HGMusicThreeCollectionViewCell class] forCellWithReuseIdentifier:@"HGMusicThreeCollectionViewCell"];
    
    [_collection registerClass:[HGMusicFourCollectionViewCell class] forCellWithReuseIdentifier:@"HGMusicFourCollectionViewCell"];
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake(SREEN_WIDTH, 200);
    }else if(section == 1){
        return CGSizeZero;
    }else{
        return CGSizeMake(SREEN_WIDTH, 30);
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return _dataArr.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        
        if (indexPath.section == 0) {
            UICollectionReusableView *header= [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionReusableView" forIndexPath:indexPath];
            
            
            
            return header;
            
        }else {
            
            HGMusicModel *model = _dataArr[indexPath.section];
            
            HGMusicHeaderCollectionReusableView *header= [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HGMusicHeaderCollectionReusableView" forIndexPath:indexPath];
            header.backgroundColor = [UIColor colorWithRed:0.94 green:1 blue:0.94 alpha:0.8];
            header.model = model;
            
            return header;
        }
    }
    
    
    return nil;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    HGMusicModel *model = _dataArr[section];
    
    if (section == 0) {
        return 0;
    }else if (section == 2){
        
        return 3;
    }else{
        return model.dataArr.count;
    }
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return CGSizeZero;
    }
    
    if (indexPath.section == 1) {
        return CGSizeMake((SREEN_WIDTH -50)/4, 50);
    }else if (indexPath.section == 2){
        return CGSizeMake(SREEN_WIDTH, 50);
    }else if (indexPath.section == 3){
        return CGSizeMake((SREEN_WIDTH - 50)/3, 140);
    }else if (indexPath.section == 4){
        return CGSizeMake((SREEN_WIDTH - 50)/3, 140);
    }else if (indexPath.section == 5){
        return CGSizeMake(SREEN_WIDTH , 100);

    }else if (indexPath.section == 6){
        return CGSizeMake((SREEN_WIDTH - 50)/3, 140);
    }else if (indexPath.section == 7){
        return CGSizeMake((SREEN_WIDTH - 50)/3, 140);

    }else{
        return CGSizeMake(SREEN_WIDTH , 100);

    }
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HGMusicModel *model = _dataArr[indexPath.section];
    NSString *style = [model.style stringValue];
    
    if ([style isEqualToString:@"12"]) {
        HGMusicOneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HGMusicOneCollectionViewCell" forIndexPath:indexPath];
        
        HGMusicDataModel *model1 = model.dataArr[indexPath.row];
        cell.model = model1;
        
        return cell;
    }else if ([style isEqualToString:@"14"]){
        HGMusicTwoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HGMusicTwoCollectionViewCell" forIndexPath:indexPath];
        
        HGMusicDataModel *dataModel = [model.dataArr firstObject];
        SongModel *model = dataModel.songsArr[indexPath.row];
        if (model.mvListArr.count == 0) {
            model.haveMV = NO;
        }else{
            model.haveMV = YES;
        }
        cell.model = model;

        
        cell.label1.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
        return cell;
    }else if ([style isEqualToString:@"5"]){
        
        HGMusicThreeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HGMusicThreeCollectionViewCell" forIndexPath:indexPath];
       
        
        HGMusicDataModel *model1 = model.dataArr[indexPath.row];
        
        cell.model = model1;
        
        return cell;
    }else if ([style isEqualToString:@"5"]){
        HGMusicThreeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HGMusicThreeCollectionViewCell" forIndexPath:indexPath];
        
        HGMusicDataModel *model1 = model.dataArr[indexPath.row];
        
        cell.model = model1;
        
        return cell;

    }else if ([style isEqualToString:@"2"]){
        
        HGMusicFourCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HGMusicFourCollectionViewCell" forIndexPath:indexPath];
        
       
        
        HGMusicDataModel *model1 = model.dataArr[indexPath.row];
        
        cell.model = model1;
        
        return cell;
        
        
        return cell;
    }else if ([style isEqualToString:@"5"]){
        HGMusicThreeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HGMusicThreeCollectionViewCell" forIndexPath:indexPath];
        
        
        HGMusicDataModel *model1 = model.dataArr[indexPath.row];
        
        cell.model = model1;
        
        return cell;

    } else if ([style isEqualToString:@"5"]){
        HGMusicThreeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HGMusicThreeCollectionViewCell" forIndexPath:indexPath];
        
        
        HGMusicDataModel *model1 = model.dataArr[indexPath.row];
        
        cell.model = model1;
        
        return cell;

    }else{
        HGMusicFourCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HGMusicFourCollectionViewCell" forIndexPath:indexPath];
        
        
        
        HGMusicDataModel *model1 = model.dataArr[indexPath.row];
        
        cell.model = model1;
        
        return cell;
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HGMusicModel *model = _dataArr[indexPath.section];
    NSString *style = [model.style stringValue];
    if ([style isEqualToString:@"5"]) {
        
        if ([model.name isEqualToString:@"新歌新碟"]) {
            //data数据的model
            HGMusicDataModel *dataModel = model.dataArr[indexPath.row];
            NSDictionary *action = dataModel.action;
            NSString *value = [NSString stringWithFormat:@"%@", action[@"value"]];

          
            HGNewMusicViewController *hgNew = [[HGNewMusicViewController alloc] init];
            
            hgNew.value = value;
            
            [self.navigationController pushViewController:hgNew animated:YES];
            
        }else if ([model.name isEqualToString:@"最热MV"]){
            //data数据的model
            HGMusicDataModel *dataModel = model.dataArr[indexPath.row];
            NSDictionary *action = dataModel.action;
            
            HGMVsecViewController *hgmv = [[HGMVsecViewController alloc] init];
            hgmv.videoId = action[@"value"];
            
           
            [self presentViewController:hgmv animated:YES completion:^{
                
            }];

        }else{
            //data数据的model
            HGMusicDataModel *dataModel = model.dataArr[indexPath.row];
            NSDictionary *action = dataModel.action;
            NSString *value = [NSString stringWithFormat:@"%@", action[@"value"]];
            
            HGMusicSecViewController *secVC = [[HGMusicSecViewController alloc] init];
            
            secVC.value = value;
            
            [self.navigationController pushViewController:secVC animated:YES];
            
            
        }
        
        
        
        
        
        
    }
    
    if ([style isEqualToString:@"2"]) {
        //data数据的model
        HGMusicDataModel *dataModel = model.dataArr[indexPath.row];
        NSDictionary *action = dataModel.action;
        NSString *value = [NSString stringWithFormat:@"%@", action[@"value"]];
        
        HGMusicSecViewController *secVC = [[HGMusicSecViewController alloc] init];
        
        secVC.value = value;
        
        [self.navigationController pushViewController:secVC animated:YES];

    }
    
    
    
    
    if ([style isEqualToString:@"14"]) {
        

        NSString *str = [NSString stringWithFormat:@"http://api.favorite.ttpod.com/favorite/song/plaza?random=0&version=0&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5S&f=f320&s=s310&imsi=&hid=&splus=9.2.1&active=1&net=2&openudid=72d26f93c0799c325993cf509c2472027f13fa1d&idfa=EA226D01-40F9-413A-A388-906DF3F490F7&utdid=Vu6ZOeT0fJwDAFqPovwyz9bO&alf=201200&bundle_id=com.ttpod.music&latitude=38.88246122520464&longtitude=121.5395609448811%ld", indexPath.row];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"type" object:style];
        
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"chuanzhi" object:str];
        
        
    }
    
   
    if (indexPath.section == 1) {
        if (indexPath.row == 1) {
            HSViewController *all = [[HSViewController alloc] init];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:all];
            [self presentViewController:nav animated:YES completion:^{
                
            }];
        }
    }
    
}


- (void)handelData{
    
    [APPTools GETWithURL:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1458477388694&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5S&f=f320&s=s310&imsi=&hid=&splus=9.2.1&active=0&net=2&openudid=72d26f93c0799c325993cf509c2472027f13fa1d&idfa=EA226D01-40F9-413A-A388-906DF3F490F7&utdid=Vu6ZOeT0fJwDAFqPovwyz9bO&alf=201200&bundle_id=com.ttpod.music&latitude=38.88249276837505&longtitude=121.5394209589833" par:nil success:^(id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        NSArray *arr = dic[@"data"];
        self.dataArr = [HGMusicModel baseModelHandleWithArr:arr];
        NSLog(@"%@", _dataArr);
        
        [self createLunBoTuView];
        [_collection reloadData];
        
        [MBProgressHUD hideHUDForView:_collection animated:YES];
        
    } filed:^(NSError *error) {
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
